package cn.lili.modules.member.serviceimpl;

import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.modules.member.entity.dos.User;
import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.modules.member.entity.dos.User;
import cn.lili.modules.member.mapper.VipActiveCodeMapper;
import cn.lili.modules.member.service.UserService;
import cn.lili.modules.member.service.VipActiveCodeService;
import cn.lili.modules.system.entity.dos.LiVipActiveCode;
import cn.lili.modules.urbansubstation.entity.dto.VipCodeSearchParams;
import cn.lili.mybatis.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author asus
* @description 针对表【li_vip_active_code(激活码表)】的数据库操作Service实现
* @createDate 2023-11-07 13:52:33
*/
@Service
@AllArgsConstructor
public class VipActiveCodeServiceImpl extends ServiceImpl<VipActiveCodeMapper, LiVipActiveCode>
    implements VipActiveCodeService {

    private final UserService userService;

    public Page<LiVipActiveCode> getCodePage (VipCodeSearchParams vipCodeSearchParams, PageVO page) {
        QueryWrapper<LiVipActiveCode> queryWrapper = vipCodeSearchParams.queryWrapper();
        queryWrapper.orderByDesc("create_time");
        return this.baseMapper.selectPage(PageUtil.initPage(page), queryWrapper);
    }

    @Override
    public LiVipActiveCode getActiveCodeByCode (String code) {
        LambdaQueryWrapper<LiVipActiveCode> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LiVipActiveCode::getActiveCode, code);
        queryWrapper.eq(LiVipActiveCode::getActiveStatus, 0);
        queryWrapper.last("limit 1");
        return this.baseMapper.selectOne(queryWrapper);
    }

    public void updateCodeStatus (LiVipActiveCode vipActiveCode, String userId) {
        LambdaUpdateWrapper<LiVipActiveCode> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(LiVipActiveCode::getActiveCode, vipActiveCode.getActiveCode());
        updateWrapper.set(LiVipActiveCode::getActiveStatus, 1);
        updateWrapper.set(LiVipActiveCode::getUserId, userId);
        updateWrapper.set(LiVipActiveCode::getTimeType, vipActiveCode.getTimeType());
        updateWrapper.set(LiVipActiveCode::getUseTime, new Date());
        User user = userService.getById(userId);
        if (null != user && CharSequenceUtil.isNotEmpty(user.getUsername())) {
            updateWrapper.set(LiVipActiveCode::getUserName, user.getUsername());
        }
        updateWrapper.set(LiVipActiveCode::getUpdateTime, new Date());
        update(updateWrapper);
    }
}




